﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

using VistaControls;

namespace PowerWaveAddin
{
	/// <summary>
	/// Gets the appropriate URL for the wave notes servelet.
	/// </summary>
	internal class UrlStrings
	{
		private const string createPresentationUrl = @"http://wave-notes.appspot.com/presentation";
		private const string addSlideUrl = @"http://wave-notes.appspot.com/slide";

		private const string debugCreatePresentationUrl = @"http://localhost:8888/presentation";
		private const string debugAddSlideUrl = @"http://localhost:8888/slide";

		public static string PresentationServiceUrl
		{
			get
			{
				if (ThisAddIn.OperationMode == ExecutionMode.HostedService)
					return createPresentationUrl;
				return debugCreatePresentationUrl;
			}
		}

		public static string SlideServiceUrl
		{
			get
			{
				if (ThisAddIn.OperationMode == ExecutionMode.HostedService)
					return addSlideUrl;
				return debugAddSlideUrl;
			}
		}
	}

	internal class PostHelpers
	{
		private static SimplePostHelper presentationPostHelper;

		public static SimplePostHelper PresentationPostHelper
		{
			get
			{
				if (presentationPostHelper == null)
				{
					presentationPostHelper = new SimplePostHelper(UrlStrings.PresentationServiceUrl);
				}

				return presentationPostHelper;
			}
		}
	}

	internal enum ExecutionMode
	{
		HostedService,
		LocalService
	}

	internal class NumericConstants
	{
		public const long MaxBatchSize = 800000;
	}

	/// <summary>
	/// Field names expected/accepted by the servlet when submitting the post request
	/// </summary>
	internal class FieldNames
	{
		public const string Creator = "creator";
		public const string EmailAddress = "email";
		public const string ExistingPresentationKey = "presentationKey";
		public const string Method = "method";
		public const string NewPresentationId = "title";
		public const string Password = "password";
		public const string PresentationKey = "presentationKey";
		public const string SlideIndex = "slideIndex";
	}

	/// <summary>
	/// Possible values to be sent to the sever as the value of the "method" field.
	/// </summary>
	class MethodFieldValues
	{
		public const string List = "list";
		public const string Sync = "sync";
	}

	/// <summary>
	/// Params to be passed to the async upload method
	/// I use this class to grou them since the DoWorkAsync method will
	/// only take one param.
	/// </summary>
	internal class UploadParams
	{
		public FileInfo[] Files { get; set; }
		public string Email { get; set; }
		public string NewName { get; set; }
	}

	internal enum UploadCode
	{
		SUCCESS,
		FAIL,
		UNPUBLISHED
	}

	internal class SystemHelper
	{
		private static string appDataFolder;
		public static string AppDataFolder
		{
			get
			{
				if (appDataFolder == null)
				{
					appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
					appDataFolder = System.IO.Path.Combine(appDataFolder, "Wave-Notes");
					if (!Directory.Exists(appDataFolder))
						Directory.CreateDirectory(appDataFolder);
				}
				return appDataFolder;
			}
		}

		public static string ConfigXmlPath
		{
			get
			{
				return Path.Combine(AppDataFolder, @"UploadConfiguration.xml");
			}
		}
	}

	internal static class StringExtension
	{
		public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
		{
			if (format == null)
				throw new ArgumentNullException("format");

			return string.Format(provider, format, args);
		}
	}

	internal class Aero
	{
		/// <summary>
		/// Returns true if Aero is supported and should be used;
		/// </summary>
		public static bool IsAeroSupported
		{
			get
			{
				return OsSupport.IsVistaOrBetter &&
				OsSupport.IsCompositionEnabled;
			}
		}

		public static void DisplayErrorMessage(
				string caption,
				string aeroDescription,
				string errorMessage)
		{
			if (Aero.IsAeroSupported)
			{
				VistaControls.TaskDialog.TaskDialog.Show(
						aeroDescription,
						caption,
						errorMessage,
						VistaControls.TaskDialog.TaskDialogButton.Close,
						VistaControls.TaskDialog.TaskDialogIcon.SecurityError);
			}
			else
			{
				MessageBox.Show(
						errorMessage,
						caption,
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
			}
		}
	}
}
